import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        song: {
            // 当前播放歌曲信息
            id: 1,
            name: "",
            artist: "",
            time: "",
            album: "",
            cover: "http://p1.music.126.net/li19i75jz6GGOT79IyAjYA==/109951165100592039.jpg",
            songUrl: "",
        },
        songList: []
    },
    mutations: {
        updateSong(state, song) {
            state.song = song
            console.log(song)
                // sessionStorage.setItem("Song", JSON.stringify(song))
        },
        updateSongList(state, payload) {
            state.songList = payload
        }
    },
    actions: {
        async a_updateSongList(context, payload) {
            await context.commit("updateSongList", payload)

            // 保存播放列表每一首歌曲的id
            await (function() {
                let array = []
                payload.forEach(element => {
                    array.push(element.id)
                });
                sessionStorage.setItem("SongList_Song__ID", JSON.stringify(array))
            })()
            sessionStorage.setItem("SongList", JSON.stringify(payload))
        },
        async a_updateSong(context, payload) {
            await context.commit("updateSong", payload)
            await sessionStorage.setItem("Song", JSON.stringify(payload))
            console.log(sessionStorage.getItem("Song"))
        }
    },
    modules: {}
})